package com.rc.evcharger.common.utils;

import io.netty.util.Timeout;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.HashMap;
import java.util.Map;


/**
 * key是桩的终端地址或者终端地址加上枪号
 * @author yang  定时器单例类
 */
public class TimeoutUtil {
    private static Logger logger = LogManager.getLogger(TimeoutUtil.class);

    private Map<String, Timeout> timeoutMap = new HashMap<String, Timeout>();
    private static TimeoutUtil timeoutUtil = null;

    private TimeoutUtil() {

    }

    public static TimeoutUtil newInstance() {
        if (timeoutUtil == null) {
            timeoutUtil = new TimeoutUtil();
        }
        return timeoutUtil;
    }

    public void addTimeout(String key, Timeout timeout) {
        logger.warn("TimeoutUtil添加定时器：" + key);
        timeoutMap.put(key, timeout);
    }

    public void removeTimeout(String key) {
        timeoutMap.remove(key);
    }

    public String getRtua(Timeout timeout) {
        for (Map.Entry<String, Timeout> entry : timeoutMap.entrySet()) {
            if (entry.getValue() == timeout) {
                return entry.getKey();
            }
        }
        return null;
    }

    public Timeout getTimeout(String key) {
        return timeoutMap.get(key);
    }

    public Map<String, Timeout> getTimeoutMap() {
        return this.timeoutMap;
    }
}
